﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Common;
using Model;

namespace AcceptServer.Service
{
    public class OperateCmdData
    {
        //    private static TaxiGpsHistoryInfo _taxiGpsHistoryInfoBuffer = new TaxiGpsHistoryInfo();


        //    private static readonly Dictionary<int, byte[]> _imageDataBuffer = new Dictionary<int, byte[]>();

        public byte[] AnalysisMessageId(byte[] realData, IPAddress clientIp)
        {
            ushort messageId = OperateAgreement.GetMessageIdInMessageHead(realData);
             //Console.WriteLine(messageId.ToString("x2"));

            string phoneNumbertemp = Common.OperateAgreement.GetPhoneNumInMessageHead(realData);

            if (phoneNumbertemp == "18602789588")
            {
                Console.WriteLine(messageId.ToString("x2"));
            }
            byte[] sendData = new byte[1];
            TaxiProtocol taxiProtocol = new TaxiProtocol();
            PhoneUserProtocol phoneUserProtocol = new PhoneUserProtocol();
            CommonProtocol commonProtocol = new CommonProtocol();
            StationProtocol stationProtocol=new StationProtocol();
            switch (messageId)
            {
                case ConstParameters.CmdPhoneUseGetNearbyTaxi:
                    {
                        sendData = commonProtocol.GetNearbyTaxiInfoData(realData, ConstParameters.TypePhoneUser);
                        break;
                    }
                case ConstParameters.CmdPhoneUseGetNearbyTaxi2:
                    {
                        sendData = commonProtocol.GetNearbyTaxiInfoData2(realData, ConstParameters.TypePhoneUser);
                        break;
                    }
                case ConstParameters.CmdTaxiGetNearbyTaxi:
                    {
                        sendData = commonProtocol.GetNearbyTaxiInfoData(realData, ConstParameters.TypeTaxi);
                        break;
                    }
                case ConstParameters.CmdTaxiGetNearbyTaxi2:
                    {
                        sendData = commonProtocol.GetNearbyTaxiInfoData2(realData, ConstParameters.TypeTaxi);
                        break;
                    }
                case ConstParameters.CmdPhoneUserUploadingTradeinfo:
                    {
                        sendData = commonProtocol.UploadingTradeinfo(realData, ConstParameters.TypePhoneUser);
                        break;
                    }
                case ConstParameters.CmdTaxiUploadingTradeinfo:
                    {
                        sendData = commonProtocol.UploadingTradeinfo(realData, ConstParameters.TypeTaxi);
                        break;
                    }
                case ConstParameters.CmdTaxiUpdataGpsInfo:
                    {
                        //  sendData = GetUploadImageGpsInfo(1);
                        taxiProtocol.TaxiUpdataGpsInfo(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdTaxiUpdataGpsInfo2:
                    {
                        //  sendData = GetUploadImageGpsInfo(1);
                        taxiProtocol.TaxiUpdataGpsInfo2(realData, clientIp);
                        break;
                    }

                case ConstParameters.CmdTaxiRegister:
                    {
                        sendData = taxiProtocol.TaxiRegister(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdTaxiRegister2:
                    {
                        sendData = taxiProtocol.TaxiRegister2(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPhoneUserRegister:
                    {
                        sendData = phoneUserProtocol.PhoneUserRegister(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPhoneUserRegister2:
                    {
                        sendData = phoneUserProtocol.PhoneUserRegister2(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdTaxiQueryAd:
                    {
                        sendData = taxiProtocol.TaxiQueryAd(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiQueryArea:
                    {
                        sendData = taxiProtocol.TaxiQueryArea(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiUpdataImageGpsInfo:
                    {
                        sendData = taxiProtocol.TaxiUpdataImageGpsInfo(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPhoneUserUpdataRegisterCode:
                    {
                        sendData = phoneUserProtocol.RecordRegisterCode(realData);
                        break;
                    }
                case ConstParameters.CmdPhoneUserGrade:
                    {
                        sendData = phoneUserProtocol.UserGrade(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiIsCalling:
                    {
                        sendData = taxiProtocol.UpdataTaxiCallingState(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiUpdataWarningInfo:
                    {
                        sendData = taxiProtocol.UpdataTaxiWarningInfo(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiGetWarningConfig:
                    {
                        sendData = taxiProtocol.GetWarningConfig(realData);
                        break;
                    }
                case ConstParameters.CmdIPhoneUserRegister:
                    {
                        sendData = phoneUserProtocol.IPhoneUserRegister(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPhoneUpdataLog:
                    {
                        phoneUserProtocol.PhoneUpdataLog(realData);
                        break;
                    }
                case ConstParameters.CmdStationGetClientVersion:
                    {
                        sendData = stationProtocol.GetClientVersionInfoData(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiGetTaxiNo:
                    {
                        sendData = taxiProtocol.GetTaxiNo(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiGetBlackList:
                    {
                        sendData = taxiProtocol.GetBlackList(realData);
                        break;
                    }
                
                case ConstParameters.CmdGetStationList:
                    {
                        sendData = stationProtocol.GetStationsListData(realData);
                        break;
                    }
                case ConstParameters.CmdStationGetClientVersionWithCurrentVersion:
                    {
                        sendData = stationProtocol.GetClientVersionInfoData(realData);
                        break;
                    }
                case ConstParameters.CmdGetStationAdUrlList:
                    {
                        sendData = stationProtocol.GetAdUrlData(realData);
                        break;
                    }
                //case ConstParameters.CmdPhoneUserGetNearbyRentCar:
                //    {
                   //    sendData = commonProtocol.GetNearbyRentCarInfoData(realData,ConstParameters.TypePhoneUser);
                //        break;
                //    }
                case ConstParameters.CmdTaxiRegister3:
                    {
                        sendData = taxiProtocol.TaxiRegister3(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPrivateCarRegistRequest://请求注册
                    {
                        sendData = taxiProtocol.PrivateRegistRequest(realData);
                        break;
                    }
                case ConstParameters.CmdPrivateCarVerifyRegistCode://验证注册码
                    {
                        sendData = taxiProtocol.PrivateCarVerifyRegistCode(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiGetNearByByCarType:
                    {
                        sendData = commonProtocol.GetNearbyTaxiInfoDataByCarType(realData, ConstParameters.TypeTaxi);
                        break;
                    }
                case ConstParameters.CmdPhoneUserGetNearByByCarType:
                    {
                        sendData = commonProtocol.GetNearbyTaxiInfoDataByCarType(realData, ConstParameters.TypePhoneUser);
                        break;
                    }    
                case ConstParameters.CmdCarSetSignature:
                    {
                        sendData = taxiProtocol.SetSignature(realData);
                        break;
                    }
                case ConstParameters.CmdStationGetNewestXmlVersion:
                    {
                        sendData = stationProtocol.GetXmlVersion(realData);
                        break;
                    }
                case ConstParameters.CmdPrivateCarSetInfos:
                    {
                        sendData = taxiProtocol.PrivateCarUpdateInfos(realData);
                        break;
                    }
                case ConstParameters.CmdPrivateCarRegister:
                    {
                        sendData = taxiProtocol.TaxiRegister4(realData, clientIp);
                        break;
                    }                
                case ConstParameters.CmdPhoneUseGetNearbyCar:
                    {
                        sendData = commonProtocol.GetNearbyCarInfoWithStateByCarType
                            (realData, ConstParameters.TypePhoneUser);
                        break;
                    }
                case ConstParameters.CmdPhoneUserRegistRequest:
                    {
                        sendData = phoneUserProtocol.PhoneUserRegistRequest(realData);
                        break;
                    }
                case ConstParameters.CmdPhoneUserVerifyRegistCode:
                    {
                        sendData = phoneUserProtocol.PhoneUserVerifyRegistCode(realData);
                        break;
                    }
                case ConstParameters.CmdGetCarClientVersion:
                    {
                        sendData = taxiProtocol.GetVersionByCarType(realData);
                        break;
                    }         
             
                case ConstParameters.CmdSpecialCarRegist:
                    {
                        sendData = taxiProtocol.SpecialCarRegister(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdUpdateGpsWithCarType:
                    {
                         taxiProtocol.CarUpdataGpsInfoWithCarType(
                            realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPasseagerCallCar:
                    {
                        sendData = phoneUserProtocol.PasseagerWantToCarCar
                            (realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPhonePlatformLogin:
                    {
                        sendData = taxiProtocol.PlatformLogin(realData, clientIp);
                        break;
                    }
                case ConstParameters.CmdPhonePlatformUpdateArea:
                    {
                        sendData = taxiProtocol.PlatformUpdateArea(realData);
                        break;
                    }

                case ConstParameters.CmdTaxiCheckIdentity:
                    {
                        sendData = taxiProtocol.CheckIdentity(realData);
                        break;
                    }
                case ConstParameters.CmdTaxiChangePasswordOfIdCard:
                    {
                        sendData = taxiProtocol.ChangePasswordOfIdCard(realData);
                        break;
                    }

            }
            return sendData;
        }








        /// <summary>
        /// 根据IP地址查询归属地然后返还对应归属地的IP地址。
        /// </summary>
        /// <returns></returns>
        private string GetLocalServerIP(string clientIP)
        {

            WebClient client = new WebClient();
            string url = "http://www.ip138.com/ips138.asp?ip=" + clientIP;
            string URL_Content = System.Text.Encoding.GetEncoding("GBK").GetString(client.DownloadData(url));
            Regex Reg = new Regex("<td align=\"center\"><ul class=\"ul1\"><li>(.*?)</li><li>(.*?)</li></ul></td>");
            Match Match = Reg.Match(URL_Content);
            string ipLocal = string.Empty;
            if (Match.Success)
            {
                ipLocal = Match.Groups[1].Value + " " + Match.Groups[2].Value;
                IniFiles myini = new IniFiles(System.Environment.CurrentDirectory + "\\system.ini");
                NameValueCollection nameValue = new NameValueCollection();
                myini.ReadSectionValues("IPCONFIG", nameValue);
                foreach (string key in nameValue.AllKeys)
                {
                    string[] locale = key.Split('|');
                    foreach (string s in locale)
                    {
                        if (ipLocal.Contains(s))
                        {
                            var strings = nameValue.GetValues(key);
                            if (strings != null) return strings[0];
                        }
                    }
                }
            }


            return string.Empty;
        }







    }
}
